package com.maotai.llzhou.thread;

import java.net.URLEncoder;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;

import org.apache.http.client.config.RequestConfig;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.llzhou.bean.MaotaiString;
import com.maotai.llzhou.bean.MaotaiWebCookie;
import com.maotai.llzhou.service.MaotaiService;
import com.maotai.llzhou.util.FaceUtil;
import com.maotai.llzhou.util.HttpClientUtilNew;
import com.maotai.llzhou.util.IPFactory;
import com.maotai.llzhou.util.JSUtil;

/**
 * 登录并且抢购
 */
public class MaotaiAuditCheckThread extends Thread {

	@Resource(name = "maotaiService")
	private MaotaiService maotaiService;
	private Map<String, Object> zhanghao;
	private String ipAndPort;
	private MaotaiWebCookie mwc;

	public MaotaiAuditCheckThread(MaotaiService maotaiService,
			Map<String, Object> zhanghao, String ipAndPort, MaotaiWebCookie mwc) {
		super();
		this.maotaiService = maotaiService;
		this.zhanghao = zhanghao;
		this.ipAndPort = ipAndPort;
		this.mwc = mwc;
	}

	public MaotaiWebCookie getMwc() {
		return mwc;
	}

	public void setMwc(MaotaiWebCookie mwc) {
		this.mwc = mwc;
	}

	public String getIpAndPort() {
		return ipAndPort;
	}

	public void setIpAndPort(String ipAndPort) {
		this.ipAndPort = ipAndPort;
	}

	public MaotaiService getMaotaiService() {
		return maotaiService;
	}

	public void setMaotaiService(MaotaiService maotaiService) {
		this.maotaiService = maotaiService;
	}

	public Map<String, Object> getZhanghao() {
		return zhanghao;
	}

	public void setZhanghao(Map<String, Object> zhanghao) {
		this.zhanghao = zhanghao;
	}

	@Override
	public void run() {
		try {
			String auth = zhanghao.get("auth")+"";
            if("".equals(auth)||"null".equals(auth)){
            	zhanghao.put("initstatus", "0");
				maotaiService.updateZhanghaoInitStatus(zhanghao);
	        	return;
            }
            
			HttpClientUtilNew hcu = HttpClientUtilNew.getInstance();
			// 增加代理
			RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
			
			/**
			 * 新增扫脸功能
			 */
			//访问预约页面
	        //信用体系
	        //信用扫描二维码
	        String auditUrl = "https://i.cmaotai.com/yundt-application-trade-core/api/v1/yundt/trade/memberdoc/audit/get";
	        mwc = hcu.sendHttpGetAppProxy(auditUrl, mwc, config, null);
	        System.out.println(zhanghao.get("code")+"----获取实名结果----" + mwc.getContent());
	        JSONObject resultCode = JSONObject.parseObject(mwc.getContent());
	        
	        if(resultCode.getIntValue("resultCode")==401){
	        	zhanghao.put("loginstatus", "0");
	        	maotaiService.updateZhanghaoLoginStatus(zhanghao);
	        	zhanghao.put("initstatus", "0");
				maotaiService.updateZhanghaoInitStatus(zhanghao);
	        	return;
	        }
            if(resultCode.getIntValue("resultCode")==-1){
				zhanghao.put("initstatus", "x");
				maotaiService.updateZhanghaoInitStatus(zhanghao);
            	return;
            }
	        if(mwc.getContent().contains("docNo")){
	        	JSONObject data = JSONObject.parseObject(mwc.getContent()).getJSONObject("data");
	        	String docNo = data.getString("docNo");
	        	String realName = data.getString("realName");
	        	zhanghao.put("docNo", docNo);
	        	zhanghao.put("realName", realName);
	        	maotaiService.insertMaotaiAudit(zhanghao);
	        	return;
	        }
	        String memberId = zhanghao.get("memberId")+"";
	      //查询预约情况
			String apply_query_url = MaotaiString.APPLY_QUERY_URL.replace("xxx", memberId);
			mwc = hcu.sendHttpGetProxy(apply_query_url, mwc, config, MaotaiString.APPLY_QUERY_REFERER);
			System.out.println(zhanghao.get("code")+" "+"账号查询预约情况--"+mwc.getContent());
			if(mwc.getContent().contains("login")){
				zhanghao.put("yuyuestatus", "0");
				maotaiService.updateZhanghaoYuYueStatus(zhanghao);
				zhanghao.put("loginstatus", "0");
				maotaiService.updateZhanghaoLoginStatus(zhanghao);
				return;
			}
			if(mwc.getContent().contains("错误")){
				zhanghao.put("yuyuestatus", "0");
				maotaiService.updateZhanghaoYuYueStatus(zhanghao);
				return;
			}
			//{"data":{"endRow":1,"firstPage":1,"hasNextPage":false,"hasPreviousPage":false,"isFirstPage":true,"isLastPage":true,"lastPage":1,"list":[{"address":"江苏省南京市江宁区天禄大道1号翠屏城73栋805室","addressId":"1179370534174015495","applyTime":"2018-07-30 12:59:27","areaCode":"320115000000","buyCycleDay":null,"channelName":null,"cityCode":"320100000000","createTime":"2018-07-30 12:59:27","detailed":"天禄大道1号翠屏城73栋805室","equipId":null,"failureDay":null,"failureTime":null,"goodsId":"1180731799931808771","goodsName":"【茅台商城预约购买】贵州茅台酒（新飞天）53%vol 500ml（商品编号：741）","goodsPicture":"https://prod-oss.emaotai.cn/supplychain-dev/test/img/jXftfKCbpH.jpg","id":"1182370414635126790","itemId":"1180731799924468740","orderNo":null,"platformId":"1","provinceCode":"320000000000","receivePhone":"13372007847","receiver":"周亮亮","registPhone":"13372007847","reviewInfo":null,"reviewLevel":"0","reviewTime":null,"reviewer":null,"shopId":"1173773178264259584","skuKv":{"香型（多选）":"酱香"},"status":"1","userId":"3586409","userName":"nickboowen"}],"navigatePages":8,"navigatepageNums":[1],"nextPage":0,"orderBy":null,"pageNum":1,"pageSize":5,"pages":1,"prePage":0,"size":1,"startRow":1,"total":1},"resultCode":0,"resultMsg":"success"}
			JSONObject jObject2 = JSONObject.parseObject(mwc.getContent());
			JSONObject data2 = jObject2.getJSONObject("data");
			JSONArray data3 = data2.getJSONArray("list");
			if (data3 == null || data3.size() == 0) {
				System.out.println(zhanghao.get("code")+" "+"账号没有预约--");
				if(resultCode.get("data")==null){
		        	zhanghao.put("initstatus", "Y");
					maotaiService.updateZhanghaoInitStatus(zhanghao);
		        }
			}else{
				if(resultCode.get("data")==null){
		        	zhanghao.put("initstatus", "N");
					maotaiService.updateZhanghaoInitStatus(zhanghao);
		        }
			}
		} catch (Exception e) {
			System.out.println(zhanghao.get("code")+"--"+e.getMessage()+" : throws auditinit exception ");
			try {
				zhanghao.put("initstatus", "0");
				maotaiService.updateZhanghaoInitStatus(zhanghao);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
	}



}
